// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#include <memory>

#include "Rules/MatchRule.h"

#include "Rules/CallExprMatchRule.h"
#include "Rules/MemberAccessMatchRule.h"
#include "Rules/RefExprMatchRule.h"
#include "Rules/TypeNameMatchRule.h"
#include "Rules/ArrayInitCallExprMatchRule.h"
#include "Rules/LetAsPatternMatchRule.h"

namespace CangjieMigrator::Rules {

template <typename Rule>
static std::unique_ptr<MatchRule> TryCreateOne(const TomlConfig &config)
{
    auto result = Rule::TryBuild(config);
    if (result) {
        return result;
    }
    return nullptr;
}

template <typename First, typename... Rest>
static std::unique_ptr<MatchRule> TryCreateWithCandidates(const TomlConfig &config)
{
    if (auto result = TryCreateOne<First>(config)) {
        return result;
    }
    if constexpr (sizeof...(Rest) > 0) {
        return TryCreateWithCandidates<Rest...>(config);
    }
    return nullptr;
}

std::unique_ptr<MatchRule> MatchRule::TryCreate(const TomlConfig &config)
{
    return TryCreateWithCandidates<
        CallExprMatchRule,
        MemberAccessMatchRule,
        RefExprMatchRule,
        TypeNameMatchRule,
        ArrayInitCallExprMatchRule,
        LetAsPatternMatchRule>(config);
}

} // namespace CangjieMigrator::Rules
